﻿Imports System.Data.Objects

Partial Public Class HotelEntities
    Private Sub OnContextCreated()
        ' Register the handler for the SavingChanges event.
        AddHandler Me.SavingChanges, AddressOf context_SavingChanges
    End Sub
    ' SavingChanges event handler.
    Private Shared Sub context_SavingChanges(ByVal sender As Object, ByVal e As EventArgs)
        ' Validate the state of each entity in the context
        ' before SaveChanges can succeed.
        For Each entry As ObjectStateEntry In DirectCast(sender, 
        ObjectContext).ObjectStateManager.GetObjectStateEntries(EntityState.Added Or
        EntityState.Modified)
            ' Find an object state entry for a SalesOrderHeader object.
            If Not entry.IsRelationship Then

                Select Case entry.Entity.GetType()
                    Case GetType(Chambre)
                        Dim chambre As Chambre = TryCast(entry.Entity, Chambre)

                        If ((chambre.NbPlaces > 12) Or (chambre.NbPlaces < 0)) Then
                            Throw New ValidationException("NbPlaces doit être >0 et < 12")
                        End If

                        If ((chambre.Tarif > 2000) Or (chambre.Tarif < 0)) Then
                            Throw New ValidationException("Tarif > 0 et < 2000")
                        End If
                    Case GetType(Reservation)
                        Dim reservation As Reservation = TryCast(entry.Entity, Reservation)
                    Case GetType(Facture)
                        Dim facture As Facture = TryCast(entry.Entity, Facture)

                End Select
            End If
        Next
    End Sub

End Class
